package com.icebergteam.warfacewidget.rank.ui;

import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.RemoteViews;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.OnTaskCompleted;
import com.icebergteam.warfacewidget.core.Parser;
import com.icebergteam.warfacewidget.core.RequestTask;
import com.icebergteam.warfacewidget.core.utils.TimeDateUtils;
import com.icebergteam.warfacewidget.rank.data.Top100ClansParser;
import com.icebergteam.warfacewidget.rank.data.Top100Parser;

	public class Top100RequestTask extends RequestTask {

		public static final String className = Top100RequestTask.class.getCanonicalName();
		static String url = "http://wf.mail.ru/dynamic/ajax/index.php?do=get_ladder_info&server=0&category=1&subcategory=0";
			
	//FIXME: fix parser for top100
		private Top100Parser top100Parser;
	//	private Top100ClansParser top100Parser;
		Bitmap bitmap;

		public Top100RequestTask(OnTaskCompleted onTaskCompleted, Context ctx, Intent intent) {
			super(onTaskCompleted, ctx, intent);
			//FIXME: fix parser for top100
			 top100Parser = new Top100Parser(
			// top100Parser = new Top100ClansParser(
					 this.intent.getExtras().getBoolean(Parser.KEY_INTENT_RESTORE));
		}

	
		@Override
		protected Integer doInBackground(Void... paramas) {
			try {
				//FIXME: url - id for request top100
				this.top100Parser.saveData(url);
			//	this.top100Parser.saveData("http://wf.mail.ru/dynamic/ajax/index.php?do=get_ladder_info&server=3&category=3&subcategory=1");
			} catch (Exception e) {
				LogManager.LogE(Top100RequestTask.className, "doInBackground", e.getLocalizedMessage());
			
				
			}
			return this.top100Parser.getStateRequest();
		}

		@Override
		protected void onPostExecute(Integer result) {
			this.intent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.TOP100_TYPE_LIST);
			if(!top100Parser.isRestoreKey()){
				this.intent.putExtra(Parser.KEY_INTENT_RESTORE, false);
			}
			WidgetProvider.setTypeCurrentService(WidgetProvider.TOP100_TYPE_LIST);
			
			super.onPostExecute(result);
		}
		
		public static void setWidgetState(RemoteViews view, int result){
				


				view.setEmptyView(R.id.clans_list_View, R.id.clans_empty_textview);
				view.setTextViewText(R.id.clans_empty_textview, (result == (Parser.REQUEST_FAILED | Top100ClansParser.DB_INVALID)) ? AppManager.getContext().getResources().getText(R.string.errorTextView ): AppManager.getContext().getResources().getText(R.string.top100_empty_list_view) );
				
				try {
					view.setTextViewText(R.id.widget_control_panel_date_TextView, String.format(
							"%s", TimeDateUtils.getDateByLocale(new Date(AppManager.getDatabase().getRankTable().getDate(url)), new Locale(AppManager.getLocaleManager().getCurrentUILangId()), "dd.MM.yy"/* HH:mm*//*"dd MMM yyyy HH:mm:ss"*/)));
					
					LogManager.LogV(Top100RequestTask.className, LogManager.getExecuteMethodName(), result == Parser.REQUEST_COMPLETED,  result == (Parser.REQUEST_FAILED ), view);
				} catch (Exception e) {
					LogManager.LogE(Top100RequestTask.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
				}

			}
	}